package com.example.testdemo.demos.web.Service.Impl;

import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Service.QRCodeService;
import com.example.testdemo.demos.web.Tool.QRCodeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.UUID;

@Service
public class QRCodeServiceImpl implements QRCodeService {

    @Value("${qrcode.logoPath}")
    private String logoPath;

    @Value("${qrcode.savePath}")
    private String savePath;
    @Value("${qrcode.accessPath}")
    private String accessPath;
    /**
     * 创建qr图片和uuid
     * 扫码登录用
     * @return
     * @throws Exception
     */
    @Override
    public Result createQRimg() throws Exception {
        Result result =new Result();
        HashMap<String,String> map =new HashMap<>();
        UUID uuid = UUID.randomUUID();
        String uuidWithoutHyphens = uuid.toString().replace("-", "");
        String qrImg =QRCodeUtil.encode(uuidWithoutHyphens,logoPath,savePath,false, uuidWithoutHyphens,accessPath);
        map.put("uuid", uuidWithoutHyphens);
        map.put("qrimg",qrImg);
        map.put("accessPath",accessPath);
        map.put("filePath",accessPath+qrImg);
        result.setCode(200);
        result.setMsg("成功");
        result.setData(map);
        return result;
   }

    @Override
    public Result generateQR (String content) throws Exception {
        Result result =new Result();
        HashMap<String,String> map =new HashMap<>();
        UUID uuid = UUID.randomUUID();
        String uuidWithoutHyphens = uuid.toString().replace("-", "");
        String qrImg =QRCodeUtil.encode(content,logoPath,savePath,false, uuidWithoutHyphens,accessPath);
        map.put("uuid", uuidWithoutHyphens);
        map.put("qrimg",qrImg);
        map.put("accessPath",accessPath);
        map.put("filePath",accessPath+qrImg);
        result.setCode(200);
        result.setMsg("成功");
        result.setData(map);
        return result;
    }



}
